package common.server.web;

import common.server.utils.Contants;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author Administrator
 * @version [版本号, 2018/3/6 0006]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Order(-3)
@WebFilter(filterName = "MDCFilter", urlPatterns = "/*")
public class MDCFilter implements Filter {
	@Override
	public void init(FilterConfig filterConfig)
			throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		String trace = ((HttpServletRequest)servletRequest).getHeader(Contants.DEFATE_TRACE_NAME);
		String serviceCode = ((HttpServletRequest)servletRequest).getHeader(Contants.DEFATE_SERVICE_CODE_NAME);
		MDC.put(Contants.DEFATE_TRACE_NAME,trace);
		MDC.put(Contants.DEFATE_SERVICE_CODE_NAME,serviceCode);
		filterChain.doFilter(servletRequest,servletResponse);
	}

	@Override
	public void destroy() {

	}
}
